//
//  ReadDetailController.m
//  Product_NEWS
//
//  Created by dllo on 16/1/5.
//  Copyright © 2016年 FCH. All rights reserved.
//

#import "ReadDetailController.h"
#import "ReadDetail.h"
#import "ReadDetailImageModel.h"
#import "NetRequest.h"
@interface ReadDetailController ()

@property(nonatomic,retain)UIWebView *webView;
@property(nonatomic,retain) ReadDetail * demodel;
@end

@implementation ReadDetailController

    -(void)dealloc{
        [_webView release];
        [_demodel release];
        [super dealloc];
        
    }
    
    - (void)viewDidLoad {
        
         self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        
        [super viewDidLoad];
        [self createwebView];
    }
    //创建webView;
    -(void)createwebView{
        self.webView =[[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self.view addSubview:self.webView];
        self.webView.delegate =self;
        NetRequest *net = [[NetRequest alloc]init];
        [net GET:[ NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html",self.model.replyid] completion:^(id operation, id respObj, NSError *error) {
            NSMutableDictionary *dic = respObj;
            self.demodel =[[ReadDetail alloc] init];
            [self.demodel setValuesForKeysWithDictionary:[dic objectForKey:self.model.replyid]];
            [self showInWebView];
            
        }];
      
        [_webView release];
    }





    - (void)showInWebView
    {
        NSMutableString *html = [NSMutableString string];
        [html appendString:@"<html>"];
        [html appendString:@"<head>"];
        [html appendFormat:@"<link rel=\"stylesheet\" href=\"%@\">",[[NSBundle mainBundle] URLForResource:@"SXDetails.css" withExtension:nil]];
        [html appendString:@"</head>"];
        
        [html appendString:@"<body>"];
        [html appendString:[self touchBody]];
        [html appendString:@"</body>"];
        
        [html appendString:@"</html>"];
        
        [self.webView loadHTMLString:html baseURL:nil];
    }
    - (NSString *)touchBody
    {
        NSMutableString *body = [NSMutableString string];
        [body appendFormat:@"<div class=\"title\">%@</div>",self.demodel.title];
        [body appendFormat:@"<div class=\"time\">%@</div>",self.demodel.ptime];
        if (self.demodel.body != nil) {
            [body appendString:self.demodel.body];
        }
        // 遍历img
        for ( NSMutableDictionary *dic  in self.demodel.img) {
            ReadDetailImageModel *detailImgModel =[[ReadDetailImageModel alloc] init];
            [detailImgModel setValuesForKeysWithDictionary:dic];
            NSMutableString *imgHtml = [NSMutableString string];
            //        NSLog(@"%@---------",detailImgModel.pixel);
            // 设置img的div
            [imgHtml appendString:@"<div class=\"img-parent\">"];
            
            // 数组存放被切割的像素
            NSArray *pixel = [detailImgModel.pixel componentsSeparatedByString:@"*"];
            CGFloat width = [[pixel firstObject]floatValue];
            CGFloat height = [[pixel lastObject]floatValue];
            // 判断是否超过最大宽度
            CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width * 0.96;
            if (width > maxWidth) {
                height = maxWidth / width * height;
                width = maxWidth;
            }
            
            NSString *onload = @"this.onclick = function() {"
            "  window.location.href = 'sx:src=' +this.src;"
            "};";
            [imgHtml appendFormat:@"<img onload=\"%@\" width=\"%f\" height=\"%f\" src=\"%@\">",onload,width,height,detailImgModel.src];
            // 结束标记
            [imgHtml appendString:@"</div>"];
            // 替换标记
            [body replaceOccurrencesOfString:detailImgModel.ref withString:imgHtml options:NSCaseInsensitiveSearch range:NSMakeRange(0, body.length)];
        }
        return body;
    }
    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
